package com.wwwarehouse.financialcenter.fragment.moneywarehouse.billingrecords;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.MessageCenterPushBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.financialcenter.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         订单终止详情
 */

public class ChargingOrderTerminationDetailsFragment extends BaseFragment implements View.OnClickListener {
    private TextView mOrderDetial;
    private Button mBtnConfirm;
    private TextView mContractTime, mStopName;
//    private TerminationOrderBean.ListBean mListBean;
    private ArrayList<String> mBusinessIdList;
    private String mContractUkid;
    private String mStopId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.financial_center_terminate_order, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mOrderDetial = $(R.id.tv_order_detial);
        mBtnConfirm = $(R.id.btn_confirm);
        mContractTime = $(R.id.tv_contractTime);
        mStopName = $(R.id.tv_stop_name);

        mBtnConfirm.setOnClickListener(this);
        mOrderDetial.setOnClickListener(this);

        if (getArguments() != null) {
            MessageCenterPushBean.MsgBean.FeatureBean featureBean = (MessageCenterPushBean.MsgBean.FeatureBean) getArguments().getSerializable(Constant.KEY_MESSAGE_CENTER_FEATURE_DETAILS);
            if (featureBean != null) {
                mContractUkid = featureBean.getContractUkid();
                mStopId = featureBean.getTerminatedContract();
            } else {
//                mListBean = getArguments().getParcelable("listbean");
                mBusinessIdList = getArguments().getStringArrayList("businessIds");
//                mContractUkid = mListBean.getContractUkid();
            }
        }
        requestHttp();
    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", mContractUkid);
//        NoHttpUtils.httpPost(ContractConstant.GET_INFLUENCETASK_DETAILS, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            if ("0".equals(commonClass.getCode())) {
                switch (what) {
                    case 0:
                        if (commonClass.getData() != null) {
                            JSONObject object = JSON.parseObject(commonClass.getData().toString());
                            String contractTime = object.getString("contractTime");
                            String stopName = object.getString("stopName");

                            mContractTime.setText(Html.fromHtml("<b><font color='#121417'>" + stopName + "</font></b>\t\t终止了\t<b><font color='#121417'>" + contractTime + "</font></b>\t生效的订单："));
                            mStopName.setText("订单调整造成的影响由" + stopName + "承担；");
                        }
                        break;
                }
            } else {
                toast(commonClass.getMsg());
            }
        }

        @Override
        public void onFailed(String result, int what) {
        }

        @Override
        public void onFinish(int what) {

        }
    };

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChargingOrderTerminationDetailsFragment) {
            mActivity.setTitle("订单终止详情");
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_confirm) {
            popFragment();

        } else if (i == R.id.tv_order_detial) {
//            ConstractOrderDetailsFragment orderDetailsFragment = new ConstractOrderDetailsFragment();
//            Bundle bundle = new Bundle();
//            bundle.putInt("flag", 1);
//            bundle.putStringArrayList("businessIds", mBusinessIdList);
//            bundle.putParcelable("listbean", mListBean);
//            bundle.putString("stopId", mStopId);
//            bundle.putString("contractUkid", mContractUkid);
//            orderDetailsFragment.setArguments(bundle);
//            pushFragment(orderDetailsFragment);

        }
    }
}
